package com.emotionalEnvelope.edu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.emotionalEnvelope.edu.core.RestResponse;
import com.emotionalEnvelope.edu.dto.UserinfoDto;
import com.emotionalEnvelope.edu.mapper.UserInFoMapper;
import com.emotionalEnvelope.edu.model.UserInFo;
import com.emotionalEnvelope.edu.param.UserInFoParam;
import com.emotionalEnvelope.edu.service.IUserInFoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

@Slf4j
@Service
public class UserInfoService extends ServiceImpl<UserInFoMapper, UserInFo> implements IUserInFoService {
    @Resource
    private UserInFoMapper userinfoMapper;

    @Override
    public RestResponse<UserinfoDto> getUserInfo(Long id) {
        UserInFo model = userinfoMapper.selectById(id);
        UserinfoDto dto = new UserinfoDto();
        if (!ObjectUtils.isEmpty(model)) {
            BeanUtils.copyProperties(model, dto);
        }
        return RestResponse.ok(dto);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse<String> del(Long id) {
        UserInFo model = userinfoMapper.selectById(id);
        if (!ObjectUtils.isEmpty(model)) {
            model.deleteById();
        }
        return RestResponse.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse<String> save(UserInFoParam inFo) {
        UserInFo model = new UserInFo();
        BeanUtils.copyProperties(inFo, model);
        super.saveOrUpdate(model);
        return RestResponse.ok();
    }
}
